/*
 * Class NPCAttack
 *
 * Version 1.0
 *
 * Friday, August 22, 2008
 *
 * Created by Palidino76
 */

package net.com.hydroPK.net.packethandler;

import net.com.hydroPK.Engine;
import net.com.hydroPK.model.Player;
import net.com.hydroPK.model.combat.PlayerCombat;
import net.com.hydroPK.npcs.NPC;

public class NPCAttack implements Packet {
    /**
     * Handles selecting the attack option on NPCs.
     *
     * @param p          The Player which the frame should be handled for.
     * @param packetId   The packet id this belongs to.
     * @param packetSize The amount of bytes being recieved for this packet.
     */
    public void handlePacket(Player p, int packetId, int packetSize) {
        if (p == null || p.stream == null) {
            return;
        }
        p.attackNpc = p.stream.readUnsignedWord();
        NPC n = Engine.npcs[p.attackNpc];
        if (n == null) {
            return;
        }
        if (p.restrictedAttack(p.attackNpc)) {
            return;
        }
        if (p.nonMultiPlace()) {
            if (n.killingCount > 0 && n.playerIndex != p.playerId) {
                p.attackNpc = 0;
                p.getActionSender().sendMessage(p, "Someone else is already fighting that.");
                return;
            }
        }
        n.playerIndex = p.playerId;
        PlayerCombat playCb = new PlayerCombat(p);
        playCb.setNpcAttack(true);
    }
}
